﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;

namespace BaseTypesControlLib.Controls
{
    public abstract class LabelAndSelectionPanel:Panel
    {
        protected TextBlock txtLabel, txtText;
        public LabelAndSelectionPanel()
        {
            txtLabel = new TextBlock(); txtLabel.IsHitTestVisible = false;
            txtLabel.TextWrapping = TextWrapping.Wrap;
            Binding b = new Binding();
            b.Path = new PropertyPath("Label");
            b.Source = this;
            txtLabel.SetBinding(TextBlock.TextProperty, b);
            txtText = new TextBlock();
            txtText.IsHitTestVisible = false;
            Children.Add(txtLabel);
            Children.Add(txtText);
        }
        static LabelAndSelectionPanel()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsArrange);
            LabelProperty = DependencyProperty.Register("Label", typeof(string),
                typeof(LabelAndSelectionPanel), fpm);
        }
        protected override Size MeasureOverride(Size availableSize)
        {            
            txtLabel.Measure(availableSize);
            txtText.Measure(availableSize);

            Size sz = new Size();
            sz.Width = availableSize.Width;
            if(txtLabel.DesiredSize.Width>(sz.Width-txtText.DesiredSize.Width))
            {
                sz.Height = txtLabel.DesiredSize.Height + txtText.DesiredSize.Height;
            }
            else 
            {
                sz.Height = txtLabel.DesiredSize.Height;
            }

            return sz;
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            txtLabel.Arrange(new Rect(0, 0, txtLabel.DesiredSize.Width, txtLabel.DesiredSize.Height));
            double y = 0;
            if (txtLabel.DesiredSize.Width > (DesiredSize.Width - txtText.DesiredSize.Width))
            {
                y = txtLabel.DesiredSize.Height;
            }
            txtText.Arrange(new Rect(DesiredSize.Width - txtText.DesiredSize.Width, y, txtText.DesiredSize.Width, txtText.DesiredSize.Height));
            return DesiredSize;
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }
        public static readonly DependencyProperty LabelProperty;
    }
}
